<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Com_tutors extends CI_Controller {
	private $per_page = 8;
	private $total_rows = 12;
	private $uri_page = '';
	private $base_page = '';
	private $uri_segment = '';
	function __construct() {
		parent::__construct();
		$this->_gallery_url  = base_url() . "upload/avartars/";
		//Lấy đường dẫn vật lý của thư mục chứa hình ảnh đươc upload
		$this->_gallery_path = realpath(APPPATH . "../upload/avartars");
	}
	function index() {
		$offset            = $this->uri->segment(2);
		$this->base_page   = $this->uri->segment(1) . '/';
		$this->uri_segment = 1;
		$action            = $this->input->post('action');
		if (!empty($action)) {
			$this->session->unset_userdata('order');
			$this->session->set_userdata('order', $action);
		}
		if ($this->session->userdata('order') == 1) {
			$this->db->order_by('mucluong', 'DESC');
		} elseif ($this->session->userdata('order') == 2) {
			$this->db->order_by('mucluong', 'ASC');
		} elseif ($this->session->userdata('order') == 3) {
			$this->db->order_by('year', 'ASC');
		} elseif ($this->session->userdata('order') == 4) {
			$this->db->order_by('year', 'DESC');
		} elseif ($this->session->userdata('order') == 5) {
			$this->session->unset_userdata('order');
		}
		$tutor = $this->common_model->getdata('users', array(
			'type' => 1,
			'activated' => 1,
			'publish' => 1
		), array(
			'id',
			'subjects',
			'phone',
			'avartar',
			'description1',
			'description2',
			'rate',
			'name',
			'profile_tit',
			'mucluong'
		), '', array(
			$this->per_page,
			$offset
		));
		if (!empty($tutor)) {
			foreach ($tutor as $giasu) {
				$danhgia[$giasu->id]            = $this->common_model->getnumber2('rating_des', array(
					'tutorid' => $giasu->id
				));
				$binhluan[$giasu->id]           = $this->common_model->getnumber2('rating_des', array(
					'tutorid' => $giasu->id,
					'publish' => 1
				));
				$data2['onestar'][$giasu->id]   = $this->common_model->getnumber2('rating_star', array(
					'tutorid' => $giasu->id,
					'onestar' => 1
				));
				$data2['twostar'][$giasu->id]   = $this->common_model->getnumber2('rating_star', array(
					'tutorid' => $giasu->id,
					'twostar' => 1
				));
				$data2['threestar'][$giasu->id] = $this->common_model->getnumber2('rating_star', array(
					'tutorid' => $giasu->id,
					'threestar' => 1
				));
				$data2['fourstar'][$giasu->id]  = $this->common_model->getnumber2('rating_star', array(
					'tutorid' => $giasu->id,
					'fourstar' => 1
				));
				$data2['fivestar'][$giasu->id]  = $this->common_model->getnumber2('rating_star', array(
					'tutorid' => $giasu->id,
					'fivestar' => 1
				));
				$data2['numstar'][$giasu->id]   = $data2['onestar'][$giasu->id] + $data2['twostar'][$giasu->id] + $data2['fourstar'][$giasu->id] + $data2['fivestar'][$giasu->id] + $data2['threestar'][$giasu->id];
				$data2['percent1'][$giasu->id]  = $this->percent($data2['onestar'][$giasu->id], $data2['numstar'][$giasu->id]);
				$data2['percent2'][$giasu->id]  = $this->percent($data2['twostar'][$giasu->id], $data2['numstar'][$giasu->id]);
				$data2['percent3'][$giasu->id]  = $this->percent($data2['threestar'][$giasu->id], $data2['numstar'][$giasu->id]);
				$data2['percent4'][$giasu->id]  = $this->percent($data2['fourstar'][$giasu->id], $data2['numstar'][$giasu->id]);
				$data2['percent5'][$giasu->id]  = $this->percent($data2['fivestar'][$giasu->id], $data2['numstar'][$giasu->id]);
				$arrstar                        = array(
					$data2['onestar'][$giasu->id],
					$data2['twostar'][$giasu->id],
					$data2['threestar'][$giasu->id],
					$data2['fourstar'][$giasu->id],
					$data2['fivestar'][$giasu->id]
				);
				$data2['sosao'][$giasu->id]     = $this->checksao($arrstar);
			}
		}
		$numturtor        = $this->common_model->getnumber2('users', array(
			'type' => 1,
			'activated' => 1,
			'publish' => 1
		));
		$this->total_rows = $numturtor;
		$this->phantrang();
		$mon             = $this->common_model->getdata('subjects', array(
			'publish' => 1
		));
		$tp              = $this->common_model->getdata('province', '', '', array(
			'order',
			'ASC'
		));
		$data['title']   = 'Tìm gia sư';
		$data['content'] = $this->load->view('com_tutors/views_tutors.php', array(
			'binhluan' => $binhluan,
			'danhgia' => $danhgia,
			'tp' => $tp,
			'tutors' => $tutor,
			'mon' => $mon,
			'numturtor' => $numturtor,
			'sosao' => $data2['sosao']
		), true);
		$this->load->view('index', $data);
	}
	function details_tutor() {
		$alias           = preg_replace("/(.html)/", '', $this->security->xss_clean($this->uri->segment(1, 0)));
		$alias2          = preg_replace("/(.html)/", '', $this->security->xss_clean($this->uri->segment(2, 0)));
		$tutorid         = substr($alias, 17);
		$data            = '';
		$thongbao        = '';
		$data2['videos'] = $this->common_model->getone('video', array(
			'tutorid' => $tutorid,
			'actived' => 1
		));
		$details         = $this->common_model->getone('users', array(
			'id' => $tutorid,
			'type' => 1,
			'activated' => 1
		));
		if ($details->publish == 1) {
			$data2['details'] = $details;
		} else {
			$data['content'] = $this->load->view('com_tutors/views_details_no_publish.php', '', true);
			$this->load->view('index', $data);
			exit();
		}
		if (!empty($data2['details'])) {
			$data2['onestar']   = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $tutorid,
				'onestar' => 1
			));
			$data2['twostar']   = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $tutorid,
				'twostar' => 1
			));
			$data2['threestar'] = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $tutorid,
				'threestar' => 1
			));
			$data2['fourstar']  = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $tutorid,
				'fourstar' => 1
			));
			$data2['fivestar']  = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $tutorid,
				'fivestar' => 1
			));
			$data2['numstar']   = $data2['onestar'] + $data2['twostar'] + $data2['fourstar'] + $data2['fivestar'] + $data2['threestar'];
			$data2['percent1']  = $this->percent($data2['onestar'], $data2['numstar']);
			$data2['percent2']  = $this->percent($data2['twostar'], $data2['numstar']);
			$data2['percent3']  = $this->percent($data2['threestar'], $data2['numstar']);
			$data2['percent4']  = $this->percent($data2['fourstar'], $data2['numstar']);
			$data2['percent5']  = $this->percent($data2['fivestar'], $data2['numstar']);
			$arrstar            = array(
				$data2['onestar'],
				$data2['twostar'],
				$data2['threestar'],
				$data2['fourstar'],
				$data2['fivestar']
			);
			$data2['sosao']     = $this->checksao($arrstar);
			$data2['city']      = $this->common_model->getone('province', array(
				'id' => $data2['details']->city
			), array(
				'id',
				'title'
			));
			$data2['huyen']     = $this->common_model->getone('district', array(
				'id' => $data2['details']->district
			), array(
				'id',
				'title'
			));
			$data2['education'] = $this->common_model->getone('users', array(
				'id' => $tutorid,
				'type' => 1,
				'activated' => 1,
				'publish' => 1
			), array(
				'cap3',
				'truongchuyen',
				'bang1',
				'chuyennganh'
			));
			$subjects           = $data2['details']->subjects;
			$data2['monhoc']    = explode(',', $subjects);
			if (!empty($data2['monhoc'])) {
				$data2['con'] = $this->common_model->getdatain('subjects', array(
					'id',
					$data2['monhoc']
				));
				if (!empty($data2['con'])) {
					foreach ($data2['con'] as $c) {
						if ($c->parentid == 1 || $c->parentid == 38 || $c->parentid == 7 || $c->parentid == 52) {
							$test = explode(',', $data2['details']->sub_test);
							array_shift($test);
							array_pop($test);
							foreach ($test as $t) {
								if ($c->id == $t) {
									$mangcha[]                     = $this->common_model->getone('subjects', array(
										'id' => $c->parentid
									))->id;
									$data2['mang'][$c->parentid][] = $c;
									$data2['huongdan'][]           = $this->common_model->getone('des_sub', array(
										'subid' => $c->id,
										'userid' => $tutorid,
										'publish' => 1
									));
								}
							}
						} else {
							$mangcha[]                     = $this->common_model->getone('subjects', array(
								'id' => $c->parentid
							))->id;
							$data2['mang'][$c->parentid][] = $c;
							$data2['huongdan'][]           = $this->common_model->getone('des_sub', array(
								'subid' => $c->id,
								'userid' => $tutorid,
								'publish' => 1
							));
						}
					}
				}
				if (!empty($mangcha)) {
					$mangcha2     = array_unique($mangcha);
					$data2['cha'] = $this->common_model->getdatain('subjects', array(
						'id',
						$mangcha2
					), array(
						'id',
						'DESC'
					));
				}
			}
			$data['title'] = 'Thông tin gia sư ' . $data2['details']->name;
			if ($alias2 == 'binh-luan' || $alias2 == 'danh-gia') {
				$data2['ratting'] = $this->common_model->getdata('rating_des', array(
					'tutorid' => $tutorid,
					'publish' => 1
				), '', array(
					'id',
					'DESC'
				), 5);
				$data['content']  = $this->load->view('com_tutors/views_details_comments.php', $data2, true);
			} elseif ($alias2 == 'email') {
				$data2['ratting'] = $this->common_model->getdata('rating_des', array(
					'tutorid' => $tutorid,
					'publish' => 1
				), '', array(
					'id',
					'DESC'
				), 5);
				$data['content']  = $this->load->view('com_tutors/views_details_email.php', $data2, true);
			} else {
				$data['content'] = $this->load->view('/views_details.php', $data2, true);
			}
		} else {
			$data2['thongbao'] = 'Gia sư đang bận hoặc đã bị ban quản trị ẩn đi';
			$data['content']   = $this->load->view('404.php', '', true);
		}
		$this->load->view('index', $data);
	}
	function lop() {
		$dat['lop']      = $this->common_model->getdata('request', array(
			'publish' => 1
		));
		$data['content'] = $this->load->view('com_tutors/lop', $dat, true);
		$this->load->view('index', $data);
	}
	function request() {
		$dat['quan']      = '';
		$dat['thongbao2'] = '';
		if (!empty($_COOKIE["yeucau"])) {
			$dat['thongbao'] = '<p style="color:red;font-size:14px;font-style:italic"><i class="icon-remove icon-1x"></i> Bạn không được gửi liên tiếp các yêu cầu, vui lòng quay lại sau 5 phút.</p>';
		}
		$dat['mon']      = $this->common_model->getdata('subjects', array(
			'publish' => 1
		));
		$dat['tp']       = $this->common_model->getdata('province', '', '', array(
			'order',
			'ASC'
		));
		$dat['quan']     = $this->common_model->getdata('district', '', '', array(
			'order',
			'ASC'
		));
		$data['content'] = $this->load->view('com_tutors/views_request.php', $dat, true);
		$this->load->view('index', $data);
	}
	// Quản lý thông tin của gia sư -------------------------------------------------------------------
	// Trang cá nhân
	function tutor_profile() {
		if ($this->session->userdata('id') != '') {
			$tutor           = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			));
			$data['content'] = $this->load->view('com_tutors/views_profile.php', array(
				'tutors' => $tutor
			), true);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
		$this->load->view('index', $data);
	}
	// CẬP NHẬT ẢNH ĐẠI DIỆN
	function profile_tutors_photo() {
		$userid = $this->session->userdata('id');
		if ($userid) {
			$photo = $this->common_model->getone('users', array(
				'id' => $userid
			), array(
				'avartar'
			));
			if (!empty($photo->avartar)) {
				$data['content'] = $this->load->view('com_tutors/views_photo.php', array(
					'avartar' => $photo
				), true);
				$this->load->view('index', $data);
			} else {
				$this->upload_photo();
			}
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
	function upload_photo() {
		$data2['users']  = $this->common_model->getdata('users', array(
			'id' => $this->session->userdata('id')
		), '');
		$data['content'] = $this->load->view('views_step_photo', $data2, true);
		$this->load->view('index', $data);
	}
	// UP ảnh đại diện
	function upload_images() {
		$user = $this->common_model->getone('users', array(
			'id' => $this->session->userdata('id')
		));
		$this->upload->initialize(array(
			"file_name" => $this->common_model->rand_string(10),
			"upload_path" => realpath(APPPATH . "../upload/avartars"),
			"allowed_types" => "jpg|jpeg|gif|png"
		));
		if (!$this->upload->do_upload('avartar')) {
			$error = array(
				$this->upload->display_errors()
			);
		} else {
			$image_data = $this->upload->data();
		}
		// Kết thúc upload ảnh
		// Crop ảnh nếu ảnh lớn
		$wimg = $image_data['image_width'];
		$himg = $image_data['image_height'];
		if ($wimg >= 320 || $himg >= 480) {
			$data['content'] = $this->load->view('views_step2_photo', array(
				'images' => $image_data
			), true);
			$this->load->view('index', $data);
		} else {
			$nameimages = $image_data['file_name'];
			$avartar    = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'avartar'
			));
			if ($avartar->avartar != null) {
				$data_update = $avartar->avartar . ',' . base_url() . 'upload/avartars/' . $nameimages;
			} else {
				$data_update = base_url() . 'upload/avartars/' . $nameimages;
			}
			if ($user->buoc2 != 1) {
				$sopoint   = $this->common_model->getone('point', array(
					'id' => 1
				))->point_img;
				$pointcong = $user->point + $sopoint;
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'point' => $pointcong
				));
			}
			$this->common_model->updatedata('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'avartar' => "$data_update",
				'buoc2' => 1
			));
			redirect(base_url() . 'gia-su/anh-dai-dien.html');
		}
	}
	function crop_images() {
		$fullpatch  = $this->input->post('fulloatch');
		$nameimages = $this->input->post('nameimages');
		$width      = $this->input->post('w');
		$height     = $this->input->post('h');
		$x_xis      = $this->input->post('x');
		$y_xis      = $this->input->post('y');
		$x2_xis     = $this->input->post('x2');
		$y2_xis     = $this->input->post('y2');
		$config     = array(
			'source_image' => $fullpatch,
			'maintain_ratio' => false,
			'x_axis' => $x_xis,
			'y_axis' => $y_xis,
			'width' => $width,
			'height' => $height
		);
		$this->load->library('image_lib', $config);
		if (!$this->image_lib->crop()) {
			echo $this->image_lib->display_errors();
		} else {
			$avartar = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'avartar',
				'buoc2'
			));
			if ($avartar->avartar != null) {
				$data_update = $avartar->avartar . ',' . base_url() . 'upload/avartars/' . $nameimages;
			} else {
				$data_update = base_url() . 'upload/avartars/' . $nameimages;
			}
			if ($avartar->buoc2 != 1) {
				$pointcong = $user->point + 10;
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'point' => $pointcong
				));
			}
			$this->common_model->updatedata('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'avartar' => "$data_update",
				'buoc2' => 1
			));
			redirect(base_url() . 'gia-su/anh-dai-dien.html');
		}
	}
	// END CẬP NHẬT
	// CẬP NHẬT MÔN HỌC
	function subjects_tutor() {
		if ($this->session->userdata('id') != '') {
			$subjects = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'subjects'
			));
			if ($subjects->subjects == '') {
				$data['content'] = $this->load->view('com_tutors/no_subjects.php', '', true);
			} else {
				$data2['subjectsid'] = explode(',', $subjects->subjects);
				$data123             = array_pop($data2['subjectsid']);
				unset($data2['subjectsid'][0]);
				$data['content'] = $this->load->view('com_tutors/subjects_profile_tutors.php', $data2, true);
			}
			$this->load->view('index', $data);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
	function update_des_mon() {
		$description = $this->input->post('content_des');
		$idmonhoc    = $this->input->post('idmon');
		$this->common_model->updatedata('des_sub', array(
			'subid' => $idmonhoc,
			'userid' => $this->session->userdata('id')
		), array(
			'description' => $description
		));
		echo 'Bạn đã cập nhật thành công! Tải lại trình duyệt để xem kết quả hoặc cập nhật mô tả các môn khác!';
	}
	function get_mon_ajax() {
		$idmonhoc = $this->input->post('idmonhoc');
		$datasub  = $this->common_model->getone('subjects', array(
			'id' => $idmonhoc
		), array(
			'id',
			'title',
			'alias'
		));
		$dessub   = $this->common_model->getone('des_sub', array(
			'subid' => $idmonhoc,
			'userid' => $this->session->userdata('id')
		));
		return $this->load->view('com_tutors/views_monhoc.php', array(
			'datasub' => $datasub,
			'dessub' => $dessub
		));
	}
	function addsubjects() {
		if ($this->session->userdata('id') != '') {
			$mon      = $this->common_model->getdata('subjects', array(
				'publish' => 1,
				'parentid' => 0
			));
			$moncon   = $this->common_model->getdata('subjects', array(
				'publish' => 1,
				'parentid !=' => 0
			));
			$sub      = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			));
			$checksub = explode(',', $sub->subjects);
			array_pop($checksub);
			unset($checksub[0]);
			foreach ($moncon as $con) {
				$m[$con->parentid][] = $con;
			}
			if (!empty($_POST['mon'])) {
				$monhoc = implode(',', $_POST['mon']);
				$users  = $this->common_model->getone('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'subjects',
					'buoc1'
				));
				$mon    = $users->subjects;
				$monmoi = '';
				if ($mon == null) {
					$monmoi = ',' . $monhoc . ',';
				} else {
					$strmon   = $sub->subjects . $monhoc . ',';
					$array    = explode(',', $strmon);
					$loctrung = array_unique($array);
					$strmoi   = implode(',', $loctrung);
					$monmoi   = $strmoi . ',';
					echo $monmoi;
				}
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'subjects' => $monmoi
				));
				foreach ($_POST['mon'] as $m) {
					$checkdes = $this->common_model->getone('des_sub', array(
						'userid' => $this->session->userdata('id'),
						'subid' => $m
					));
					if (empty($checkdes)) {
						$this->common_model->insertdata('des_sub', array(
							'userid' => $this->session->userdata('id'),
							'subid' => $m
						));
					}
				}
				if ($users->buoc1 == 0) {
					$this->common_model->updatedata('users', array(
						'id' => $this->session->userdata('id')
					), array(
						'buoc1' => 1
					));
				}
				redirect(base_url() . 'gia-su/mon-hoc.html');
			}
			$data['content'] = $this->load->view('com_tutors/views_addsubjects.php', array(
				'mon' => $mon,
				'moncon' => $moncon,
				'm' => $m,
				'check' => $checksub
			), true);
			$this->load->view('index', $data);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
	// HỌC PHÍ
	function ratetutor() {
		if ($this->session->userdata('id') != '') {
			$rate            = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'mucluong',
				'id',
				'details_luong',
				'daynhieu',
				'tiendaynhieu'
			));
			$data['content'] = $this->load->view('com_tutors/views_rate.php', array(
				'rate' => $rate
			), true);
			$this->load->view('index', $data);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
	// BẰNG CẤP 
	function education() {
		$data_edu        = $this->common_model->getone('users', array(
			'id' => $this->session->userdata('id')
		), array(
			'cap3',
			'chuyennganh',
			'bang1',
			'bang2',
			'jobs'
		));
		$data['content'] = $this->load->view('com_tutors/views_education.php', array(
			'edu' => $data_edu
		), true);
		$this->load->view('index', $data);
	}
	// TỰ THUẬT
	function response() {
		if ($this->session->userdata('id')) {
			$users           = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			), array(
				'profile_tit',
				'description1',
				'description2'
			));
			$data['content'] = $this->load->view('com_tutors/views_tuthuat.php', array(
				'user' => $users
			), true);
			$data['title']   = 'Chỉnh sửa tiêu đề - Tự Thuật';
		} else {
			$data['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng này';
			$data['url']      = base_url() . 'dang-nhap.html';
			$data['content']  = $this->load->view('mod_account/views_login.php', $data, true);
		}
		$this->load->view('index', $data);
	}
	// END CẬP NHẬT
	// CÔNG VIỆC GIA SƯ
	function tutor_email() {
		if ($this->session->userdata('id')) {
			// Email
			$data['content'] = $this->load->view('com_tutors/views_email.php', '', true);
			$this->load->view('index', $data);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
	function quanlyhs() {
		$email = $this->session->userdata('email');
		if (!empty($email)) {
			$dat['mail']     = $this->common_model->getdata('gv_hs', array(
				'email_t' => $email
			));
			$data['content'] = $this->load->view('com_tutors/quanlyhs', $dat, true);
		} else {
			$dat['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng quản lý học sinh';
			$dat['url']      = 'gia-su/quan-ly-hoc-sinh.html';
			$data['content'] = $this->load->view('mod_account/views_login', $dat, true);
		}
		$this->load->view('index', $data);
	}
	function quanlygv() {
		$email = $this->session->userdata('email');
		if (!empty($email)) {
			$dat['mail']     = $this->common_model->getdata('gv_hs', array(
				'email_p' => $email
			));
			$data['content'] = $this->load->view('com_tutors/quanlygv', $dat, true);
		} else {
			$dat['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng này';
			$dat['url']      = 'tai-khoan/giao-vien.html';
			$data['content'] = $this->load->view('mod_account/views_login', $dat, true);
		}
		$this->load->view('index', $data);
	}
	function lopcangiasu() {
		$offset            = $this->uri->segment(3);
		$this->base_page   = $this->uri->segment(1) . '/' . $this->uri->segment(2) . '/';
		$this->uri_segment = 3;
		$dat['lop']        = $this->common_model->getdata('request', array(
			'publish' => 1
		), '', array(
			'id',
			'DESC'
		), array(
			$this->per_page,
			$offset
		));
		$dat['number']     = $this->common_model->getnumber2('request', array(
			'publish' => 1
		));
		$this->total_rows  = $dat['number'];
		$this->phantrang2();
		if ($this->session->userdata('id')) {
			$user     = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			));
			$subjects = explode(",", $user->subjects);
			$subtests = explode(",", $user->sub_test);
			foreach ($subjects as $subjects) {
				if (!empty($subjects)) {
					$dat['subjects'][] = $subjects;
				}
			}
			foreach ($subtests as $subtests) {
				if (!empty($subtests)) {
					$dat['subtests'][] = $subtests;
				}
			}
		}
		$data['content'] = $this->load->view('views_lopcangiasu.php', $dat, true);
		$this->load->view('index', $data);
	}
	function nhanday() {
		$to = preg_replace("/(.html)/", '', $this->security->xss_clean($this->uri->segment(3, 0)));
		if ($this->session->userdata('id') != '') {
			$user   = $this->common_model->getone('users', array(
				'id' => $this->session->userdata('id')
			));
			$monday = $user->subjects;
			$arrsub = explode(',', $monday);
			array_pop($arrsub);
			unset($arrsub[0]);
			if (!empty($arrsub)) {
				foreach ($arrsub as $su) {
					$title_sub  = $this->common_model->getone('subjects', array(
						'id' => $su
					));
					$data_sub[] = $title_sub->title;
				}
				$strsub = implode(',', $data_sub);
			} else {
				$strsub = 'Chưa có môn dạy được cập nhật';
			}
			$infost   = $this->common_model->getone('request', array(
				'id' => $to
			));
			$title    = $this->input->post('title');
			$content  = $this->input->post('noidung');
			$avartar  = explode(',', $user->avartar);
			$thongbao = '';
			$this->load->library('form_validation');
			$this->form_validation->set_rules('title', 'Tiêu đề ', 'required|xss_clean');
			$this->form_validation->set_rules('noidung', 'Nội dung ', 'required|xss_clean');
			if ($this->form_validation->run() == FALSE) {
			} else {
				$this->common_model->insertdata('email', array(
					'email_from' => $user->email,
					'email_to' => $infost->email,
					'subjects' => $title,
					'description' => $content
				));
				$thongbao = '<h2 style="color:green"><i class="icon-2x icon-check-sign"></i>&nbsp;&nbsp;&nbsp;&nbsp;Bạn đã gửi email thành công</h2>';
			}
			$email2 = $this->common_model->getdata('email', array(
				'publish' => 1,
				'email_to' => $this->session->userdata('email')
			), '', array(
				'date',
				'DESC'
			));
			if (!empty($email2)) {
				foreach ($email2 as $m) {
					$mang1[] = $m->email_from;
				}
				$mang2 = array_unique($mang1);
				foreach ($mang2 as $mang3) {
					$this->db->order_by('id', 'DESC');
					$email[] = $this->common_model->getone('email', array(
						'publish' => 1,
						'email_to' => $this->session->userdata('email'),
						'email_from' => $mang3
					), '', array(
						'date',
						'DESC'
					), '', array(
						'email_from' => $this->session->userdata('email'),
						'email_to' => $mang3
					));
				}
			}
			$data['content'] = $this->load->view('mod_email/views_nhanday.php', array(
				'user' => $user,
				'info' => $infost,
				'to' => $to,
				'thongbao' => $thongbao,
				'email' => $email
			), true);
		} else {
			$dat['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng này';
			$dat['url']      = 'gia-su/nhan-day/' . $to . '.html';
			$data['content'] = $this->load->view('mod_account/views_login', $dat, true);
		}
		$this->load->view('index', $data);
	}
	function hs_danhgia() {
		if ($this->session->userdata('id') != '') {
			$data2['reviews']   = $this->common_model->getdata('rating_des', array(
				'publish' => 1,
				'tutorid' => $this->session->userdata('id')
			), '', array(
				'id',
				'DESC'
			));
			$data2['onestar']   = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $this->session->userdata('id'),
				'onestar' => 1
			));
			$data2['twostar']   = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $this->session->userdata('id'),
				'twostar' => 1
			));
			$data2['threestar'] = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $this->session->userdata('id'),
				'threestar' => 1
			));
			$data2['fourstar']  = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $this->session->userdata('id'),
				'fourstar' => 1
			));
			$data2['fivestar']  = $this->common_model->getnumber2('rating_star', array(
				'tutorid' => $this->session->userdata('id'),
				'fivestar' => 1
			));
			$data2['numstar']   = $data2['onestar'] + $data2['twostar'] + $data2['fourstar'] + $data2['fivestar'] + $data2['threestar'];
			$data2['percent1']  = $this->percent($data2['onestar'], $data2['numstar']);
			$data2['percent2']  = $this->percent($data2['twostar'], $data2['numstar']);
			$data2['percent3']  = $this->percent($data2['threestar'], $data2['numstar']);
			$data2['percent4']  = $this->percent($data2['fourstar'], $data2['numstar']);
			$data2['percent5']  = $this->percent($data2['fivestar'], $data2['numstar']);
			$data['content']    = $this->load->view('com_tutors/views_giasu_danhgiahocsinh.php', $data2, true);
			$data['title']      = 'Đánh giá từ học sinh';
		} else {
			$dat['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng này';
			$dat['url']      = 'gia-su/danh-gia-tu-hoc-sinh.html';
			$data['content'] = $this->load->view('mod_account/views_login', $dat, true);
		}
		$this->load->view('index', $data);
	}
	function videos() {
		$videolink        = $this->input->post('videolink');
		$data['thongbao'] = '';
		if ($this->session->userdata('id') != '') {
			$check = $this->common_model->getone('video', array(
				'tutorid' => $this->session->userdata('id')
			));
			$this->load->library('form_validation');
			$this->form_validation->set_rules('videolink', 'Đường link', 'required');
			$this->form_validation->set_error_delimiters('<span style="color:red;text-align:center;display:block;margin-bottom:15px;"><i class="icon-2x icon-remove"></i>', '</span>');
			if ($this->form_validation->run() == FALSE) {
			} else {
				if (!empty($check)) {
					$this->common_model->updatedata('video', array(
						'tutorid' => $this->session->userdata('id')
					), array(
						'link' => $videolink
					));
				} else {
					$this->common_model->insertdata('video', array(
						'tutorid' => $this->session->userdata('id'),
						'link' => $videolink,
						'actived' => 0
					));
				}
				$data['thongbao'] = '<h3 style="color:green;text-align:center;display:block;margin-bottom:15px;"><i class="icon-2x icon-ok-sign"></i>&nbsp;&nbsp;&nbsp;Bạn đã cập nhật video giới thiệu thành công ! Video giới thiệu của bạn sẽ được hiển thi ngay khi ban quản trị duyệt!Vui lòng tải lại trình duyệt để xem trước video của bạn</h3>';
			}
			$data['title']   = 'Quản lí video giới thiệu';
			$data['content'] = $this->load->view('com_tutors/views_videos.php', array(
				'link' => $check,
				'thongbao' => $data['thongbao']
			), true);
		} else {
			$dat['thongbao'] = 'Bạn cần đăng nhập để sử dụng chức năng này';
			$dat['url']      = 'gia-su/video-gioi-thieu.html';
			$data['content'] = $this->load->view('mod_account/views_login', $dat, true);
		}
		$this->load->view('index', $data);
	}
	function phantrang() {
		$this->load->library('pagination');
		$config['base_url']    = base_url() . $this->uri->segment(1);
		$config['total_rows']  = $this->total_rows;
		$config['per_page']    = $this->per_page;
		$config['uri_segment'] = 2;
		$config['num_links']   = 12;
		$this->pagination->initialize($config);
	}
	function phantrang2() {
		$this->load->library('pagination');
		$config['base_url']    = base_url() . $this->uri->segment(1) . '/' . $this->uri->segment(2);
		$config['total_rows']  = $this->total_rows;
		$config['per_page']    = $this->per_page;
		$config['uri_segment'] = 3;
		$config['num_links']   = 12;
		$this->pagination->initialize($config);
	}
	function percent($num, $sum) {
		if ($num != 0) {
			$percent = ($num / $sum) * 100;
			return $percent;
		} else {
			return 0;
		}
	}
	function checksao($arrsao = array('')) {
		$max = max($arrsao);
		$i   = 0;
		foreach ($arrsao as $sao1) {
			$i++;
			if ($sao1 == $max) {
				$sao = $i;
			}
		}
		return $sao;
	}
	function hoso() {
		$user = $this->common_model->getone('users', array(
			'id' => $this->session->userdata('id')
		));
		$dat  = array();
		if ($this->input->post('submit')) {
			$file_name = array();
			foreach ($_FILES as $file) {
				$file_name[] = $this->common_model->rand_string(10);
			}
			$this->upload->initialize(array(
				"file_name" => $file_name,
				"upload_path" => realpath(APPPATH . "../upload/profiles"),
				"allowed_types" => "jpg|jpeg|gif|png"
			));
			if (!empty($_FILES['bangcap']) && $this->upload->do_multi_upload('bangcap')) {
				$ul_bangcap = $this->upload->get_multi_upload_data();
				$filename   = '';
				foreach ($ul_bangcap as $k => $value) {
					if (!empty($user->bangcap)) {
						$filename = $user->bangcap . ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					} elseif ($k == 0) {
						$filename = base_url() . 'upload/profiles/' . $value['file_name'];
					} else {
						$filename .= ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					}
				}
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'bangcap' => $filename
				));
			}
			if (!empty($_FILES['bangdiem']) && $this->upload->do_multi_upload('bangdiem')) {
				$ul_bangdiem = $this->upload->get_multi_upload_data();
				$filename    = '';
				foreach ($ul_bangdiem as $k => $value) {
					if (!empty($user->bangdiem)) {
						$filename = $user->bangdiem . ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					} elseif ($k == 0) {
						$filename = base_url() . 'upload/profiles/' . $value['file_name'];
					} else {
						$filename .= ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					}
				}
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'bangdiem' => $filename
				));
			}
			if (!empty($_FILES['thanhtich']) && $this->upload->do_multi_upload('thanhtich')) {
				$ul_thanhtich = $this->upload->get_multi_upload_data();
				$filename     = '';
				foreach ($ul_thanhtich as $k => $value) {
					if (!empty($user->thanhtich)) {
						$filename = $user->thanhtich . ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					} elseif ($k == 0) {
						$filename = base_url() . 'upload/profiles/' . $value['file_name'];
					} else {
						$filename .= ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					}
				}
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'thanhtich' => $filename
				));
			}
			if (!empty($_FILES['chungchi']) && $this->upload->do_multi_upload('chungchi')) {
				$ul_chungchi = $this->upload->get_multi_upload_data();
				$filename    = '';
				foreach ($ul_chungchi as $k => $value) {
					if (!empty($user->chungchi)) {
						$filename = $user->chungchi . ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					} elseif ($k == 0) {
						$filename = base_url() . 'upload/profiles/' . $value['file_name'];
					} else {
						$filename .= ',' . base_url() . 'upload/profiles/' . $value['file_name'];
					}
				}
				$this->common_model->updatedata('users', array(
					'id' => $this->session->userdata('id')
				), array(
					'chungchi' => $filename
				));
			}
		}
		$dat['bangcap']   = $user->bangcap;
		$dat['bangdiem']  = $user->bangdiem;
		$dat['thanhtich'] = $user->thanhtich;
		$dat['chungchi']  = $user->chungchi;
		$data['content']  = $this->load->view('mod_account/views_details_hoso', $dat, true);
		$this->load->view('index', $data);
	}
}
?>